#ifndef __LINKEDLIST_CPP_H__
#define __LINKEDLIST_CPP_H__


template<class DType>
struct LinkedList{
	typedef LinkedList<DType>* ListType;

	DType data;
	ListType next;

	LinkedList(DType data, ListType next);
	~LinkedList();

	ListType push(const DType x);
	ListType head();

	int isEmpty();
	void dump();
};


template<class DType>
LinkedList<DType>::LinkedList(DType data, ListType next) {
	this->data = data;
	this->next = next;
}

template<class DType>
LinkedList<DType>::~LinkedList() {
	if (next) {
		delete next;
		next = NULL;
	}
}

template<class DType>
LinkedList<DType>* LinkedList<DType>::push(const DType x) {
	auto prev = this;
	while (prev->next)
		prev = prev->next;
	prev->next = new LinkedList<DType>(x, NULL);
	return this;
}

template<class DType>
LinkedList<DType>* LinkedList<DType>::head() {
	return next;
}

template<class DType>
int LinkedList<DType>::isEmpty() {
	return next == NULL;
}

template<class DType>
void LinkedList<DType>::dump() {
	auto head = next;
	int n = 0;
	while (head)
	{
		if (n)printf(" ");
		else ++n;
		printf("%d", head->data);
		head = head->next;
	}
	if (next == NULL)
		printf("NULL");
	printf("\n");
}


#endif //__LINKEDLIST_CPP_H__